#include "MainImagePanel.h"
#include "../../controller/SignalsBus.h"
#include "../../models/ProjectData.h"
#include "../../models/AlgorithmData.h"


#include "../../Algorithm/PearAlgorithm.h"

#include <QGridLayout>
#include <QtConcurrent/QtConcurrent>

MainImagePanel::MainImagePanel(QWidget *parent)
    : QWidget{parent}
{
    layout = new QGridLayout();
    initComponent();
    // connect(SignalsBus::getInstance(),&SignalsBus::onChangeDisplayMode,this,&MainImagePanel::onChangeDisplayMode);
    connect(SignalsBus::getInstance(),&SignalsBus::onSingleImageChecked,this,&MainImagePanel::onDisplaySingleResult);
    connect(SignalsBus::getInstance(),&SignalsBus::multipleCheckEnd,this,&MainImagePanel::onDisplayMultipleResults);
    connect(SignalsBus::getInstance(),&SignalsBus::loadImageFolder,this,&MainImagePanel::onLoadImageFolder);
    connect(SignalsBus::getInstance(),&SignalsBus::resultMapClicked,this,&MainImagePanel::onDisplayTargetImage);
}

void MainImagePanel::onDisplayImage(const QString &image)
{

}



void MainImagePanel::onChangeDisplayMode(int mode)
{
    displayMode = mode;

    switch(mode)
    {
    case 0:
        singleImageMode();
        break;
    case 1:
        multipleImageMode();
        break;
    }
}

void MainImagePanel::onDisplaySingleResult()
{
    imageWidget->displayImage(AlgorithmParameters::getInstance()->resultImage);
}

void MainImagePanel::onDisplayMultipleResults()
{
    if(!ProjectData::getInstance()->algorithmHash.empty())
    {
        for(const auto& item:ProjectData::getInstance()->algorithmHash)
        {
            imageWidget->displayImage(item->resultImage);

            resultImages.insert(ProjectData::getInstance()->algorithmHash.key(item),item->resultImage);
        }
    }

    // if(!ProjectData::getInstance()->algorithmHash.empty())
    // {
    //     for(const auto& item:ProjectData::getInstance()->algorithmHash)
    //     {
    //         auto feature = QtConcurrent::run([=](){
    //             imageWidget->displayImage(item->resultImage);
    //         });

    //         // feature.waitForFinished();
    //     }
    // }


}

void MainImagePanel::onLoadImageFolder(const QStringList& images)
{
    try{
    if(!images.empty())
    {
        for(const auto& item:images)
        {
            HalconCpp::HObject image;
            HalconCpp::HTuple name(item.toStdString().c_str());
            // qDebug()<<"loade image: "<<name.ToString();
            HalconCpp::ReadImage(&image,name);
        }

    }
    }catch(HalconCpp::HException exception)
    {
        QString msg;
        msg = QString("Error Code: %1 \n Error Message: %2").arg(exception.ErrorCode()).arg(exception.ErrorMessage().Text());
        qDebug()<<msg;
    }
}

void MainImagePanel::onDisplayTargetImage(int cursor)
{
    if(!resultImages.empty())
    {
        imageWidget->displayImage(resultImages[cursor]);
    }
}

void MainImagePanel::initComponent()
{
    imageWidget = new HalconWidget();
    layout->addWidget(imageWidget,0,0,1,1);
    setLayout(layout);
    halconImages.insert(0,imageWidget);

    connect(SignalsBus::getInstance(),&SignalsBus::onLoadSingleImage,imageWidget,&HalconWidget::onDisplayImage);
}

void MainImagePanel::singleImageMode()
{
    if(!halconImages.isEmpty())
    {
        for(auto item:std::as_const(halconImages))
        {
            layout->removeWidget(item);
            delete item;
            item = nullptr;
        }
        halconImages.clear();
    }

    auto imageWidget = new HalconWidget();
    layout->addWidget(imageWidget,0,0,1,1);
    setLayout(layout);

    connect(SignalsBus::getInstance(),&SignalsBus::onLoadSingleImage,imageWidget,&HalconWidget::onDisplayImage);

    halconImages.insert(0,imageWidget);
}

void MainImagePanel::multipleImageMode()
{

}
